package com.zhc.validate.processor;

import com.zhc.validate.annotation.Solution;
import com.zhc.validate.constants.Constant;
import com.zhc.validate.entity.Clazz;
import lombok.extern.slf4j.Slf4j;
import org.reflections.Reflections;

import java.util.HashSet;
import java.util.Set;

@Slf4j
public class SolutionAnnotationProcessor {
    public static Set<Clazz> processor() {
        Set<Clazz> clazzSet = new HashSet<>();
        try {
            // 扫描包下所有类
            Reflections reflections = new Reflections(Constant.PACKAGE_NAME);

            // 获取被@Solution注解标注的类
            Set<Class<?>> classes = new HashSet<>(reflections.getTypesAnnotatedWith(Solution.class));

            // 获取注解熟悉 priority 的值
            classes.forEach(cls -> {
                Solution solution = cls.getAnnotation(Solution.class);
                Clazz clazz = new Clazz(cls, solution.priority());
                clazzSet.add(clazz);
            });

            return clazzSet;
        } catch (Exception e) {
            log.error("Error occurred while processing classes: {}", e.getMessage(), e);
            return Set.of();
        }
    }
}
